package com.bw.module_main

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.bw.lib_storage.AppMMKV
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class GuideActivity : AppCompatActivity() {
    
    // 引导页数据
    private val guideItems = listOf(
        GuideItem(R.layout.guide_page1, "精彩内容"),
        GuideItem(R.layout.guide_page2, "流畅体验"),
        GuideItem(R.layout.guide_page3, "个性化推荐"),
        GuideItem(R.layout.guide_page4, "开始使用")
    )
    
    // 视图引用
    private lateinit var viewpager: ViewPager2
    private lateinit var indicator: LinearLayout
    private lateinit var btn_experience: androidx.appcompat.widget.AppCompatButton
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide)

        // 获取视图引用
        viewpager = findViewById(R.id.viewpager)
        indicator = findViewById(R.id.indicator)
        btn_experience = findViewById(R.id.btn_experience)
        
        // 设置ViewPager2适配器
        viewpager.adapter = GuidePagerAdapter(this, guideItems)
        
        // 页面变化监听，更新指示器
        viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                // 更新指示器圆点状态
                updateIndicator(position)
                
                // 最后一页隐藏指示器，显示立即体验按钮
                if (position == guideItems.size - 1) {
                    indicator.visibility = View.GONE
                    btn_experience.visibility = View.VISIBLE
                } else {
                    indicator.visibility = View.VISIBLE
                    btn_experience.visibility = View.GONE
                }
            }
        })
        
        // 立即体验按钮点击事件
        btn_experience.setOnClickListener {
            // 标记已看过引导页
            AppMMKV.appGuide = false
            
            // 跳转到主页面
            startActivity(Intent(this, NavigationMainActivity::class.java))
            finish()
        }
    }
    
    // 更新指示器圆点状态
    private fun updateIndicator(selectedPosition: Int) {
        val indicatorLayout = indicator as LinearLayout
        for (i in 0 until indicatorLayout.childCount) {
            val dot = indicatorLayout.getChildAt(i)
            dot.setBackgroundResource(
                if (i == selectedPosition) {
                    R.drawable.indicator_dot_selected
                } else {
                    R.drawable.indicator_dot_normal
                }
            )
        }
    }
    
    // 引导页数据类
    data class GuideItem(val layoutResId: Int, val title: String)
}